
package servlets;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Enumeration;
import java.util.Properties;


@WebServlet("/check")
public class CheckServlet extends HttpServlet {
    Connection connection = null;
    PreparedStatement preparedStatement;

    {
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("user"),
                    properties.getProperty("pwd"));
            preparedStatement = connection.prepareStatement("SELECT sno,password FROM student WHERE sno = ? AND password = ?");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        String password = req.getParameter("password");

        try {
            preparedStatement.setString(1, user);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
//                  ServletContext servletContext = getServletContext();
                //  String contextPath = servletContext.getContextPath();
                //  System.out.println(contextPath);
//                String realPath = servletContext.getRealPath("/servlet");
//                System.out.println(realPath);
                HttpSession session = req.getSession();
                session.setAttribute("isLogin", "true");
                //  session.setMaxInactiveInterval(10);
                //  session.invalidate();
//                ServletContext servletContext = getServletContext();
//                servletContext.setAttribute("ckey","cpwd");
                ServletContext servletContext = getServletContext();
//                Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
//                while (initParameterNames.hasMoreElements()){
//                    String s = initParameterNames.nextElement();
//                    System.out.println(s);
//                }
//                String key1 = servletContext.getInitParameter("key1");
//                System.out.println(key1);
                resp.sendRedirect("grade");
            } else {
                resp.sendRedirect("login?key=true");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
//第一次注释
//第二次注释
//分支修改
System.out.print("!!!");
}
